package org.finesys.system.controller;

import java.util.List;

import org.finesys.common.core.constants.RestApiConfig;
import org.finesys.common.core.constants.ValidationGroup;
import org.finesys.common.core.module.R;
import org.finesys.common.core.util.ValidationUtil;
import org.finesys.common.log.core.annotation.LogAnnotation;
import org.finesys.common.log.core.enums.OperateType;
import org.finesys.system.api.entity.SysRouteConf;
import org.finesys.system.constants.ControllerRestApiConfig;
import org.finesys.system.service.SysRouteConfService;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

@Tag(name = "路由管理")
@RestController
@RequestMapping(ControllerRestApiConfig.ROUTE_CONF)
@RequiredArgsConstructor
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SysRouteConfController {

    private final SysRouteConfService sysRouteConfService;

 

    /**
     * 新增或修改
     */
    @Operation(summary = "修改路由信息")
    @PostMapping(value = RestApiConfig.UPDATE)
    @PreAuthorize("@pms.hasPermission('sys_route_update')")
    public R<Void> update(@RequestBody JSONObject routeConf) {
        return R.judge(sysRouteConfService.updateRouteConf(routeConf));
    }
    /**
     * 修改
     */
    @Operation(summary = "修改路由信息")
    @PutMapping(value = "updateAll")
    @PreAuthorize("@pms.hasPermission('sys_route_update')")
    public R<Void> updateAll(@RequestBody JSONArray routeConf) {
        return R.judge(sysRouteConfService.updateRouteConf(routeConf));
    }
    /**
     * 删除
     */
    @Operation(summary = "删除路由信息")
    @LogAnnotation(title = "删除路由信息", operateType = OperateType.DELETE)
    @DeleteMapping(value = RestApiConfig.DELETE)
    @PreAuthorize("@pms.hasPermission('sys_route_delete')")
    public R<?> delete(@RequestBody Long[] ids) {
        return R.success(sysRouteConfService.deleteRouteConf(ids));
    }

    /**
     * 获取单条
     */
    @Operation(summary = "获取单条")
    @GetMapping(value = RestApiConfig.GET)
    @PreAuthorize("@pms.hasPermission('sys_route_view')")
    public R<JSONObject> findById(Long id) {
        return R.success(toJson(sysRouteConfService.getById(id)));
    }

    private JSONObject toJson(SysRouteConf route) {
        JSONObject json =JSONUtil.parseObj(route);
        if(StringUtils.hasText(route.getPredicates())){
            json.set("predicates", JSONUtil.parseArray(route.getPredicates()));
        }
        if(StringUtils.hasText(route.getFilters())){
            json.set("filters", JSONUtil.parseArray(route.getFilters()));
        }
        if(StringUtils.hasText(route.getMetadata())){
            json.set("metadata", JSONUtil.parseObj(route.getMetadata()));
        }
        return json;
    }
     /**
     * 通过条件查询岗位信息表
     *
     * @param queryPost
     * @return R
     */
    @Operation(summary = "通过条件查询", description = "通过条件查询")
    @GetMapping(RestApiConfig.DETAILS)
    @PreAuthorize("@pms.hasPermission('sys_route_view')")
    public R<?> details(SysRouteConf sysRouteConf) {
        return R.success(sysRouteConfService.getOne(sysRouteConf));
    }
    /**
     * 获取全部
     */
    @Operation(summary = "获取全部")
    @GetMapping(value = RestApiConfig.ALL)
    @PreAuthorize("@pms.hasPermission('sys_route_view')")
    public R<List<SysRouteConf>> findAll() {
        List<SysRouteConf> list = sysRouteConfService.findAll();
        return R.success(list);
    }

    /**
     * 分页
     */
    @Operation(summary = "分页")
    @GetMapping(value = RestApiConfig.PAGE)
    @PreAuthorize("@pms.hasPermission('sys_route_view')")
    public R<IPage<SysRouteConf>> page(Page<SysRouteConf> page, SysRouteConf sysRouteConf) {
        ValidationUtil.validateParam(sysRouteConf, ValidationGroup.query.class);
        return R.success(sysRouteConfService.findPage(page, sysRouteConf));
    }

}
